﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YiLe.BLL.Service;
using YiLe.Model.Domain;
using Spring.Context;

namespace YiLe.View.Table
{
    public partial class TableListForm : Form
    {
        private IXs_TableService tableService;
        private IXs_CodeService codeService;

        public TableListForm(Spring.Context.IApplicationContext ctx)
        {
            InitializeComponent();

            tableService = ctx["Xs_TableService"] as IXs_TableService;
            codeService = ctx["XS_CodeService"] as IXs_CodeService;
        }

        /// <summary>
        /// TODO: 此处应该从xs_code表中读取出数据字典信息
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private static Color ConvertTypeToColor(String type)
        {
            switch (type)
            {
                case "1": //空闲
                    return Color.Green;
                case "2": //开台
                    return Color.Orange;
                case "3": //买单
                    return Color.Purple;
                case "4": //预订
                    return Color.LightGreen;
                case "5": //脏台
                    return Color.Gray;
                case "6": //停用
                    return Color.Gray;
                default:
                    break;
            }
            return Color.Gray;
        }

        /// <summary>
        /// 桌台点击事件，更改保存ID的tb内容，修改点击之后的样式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void View_TableAreaClick(object sender, EventArgs e)
        {
            Panel panel = sender as Panel;
            if (panel.BorderStyle == BorderStyle.Fixed3D)
            {
                panel.BorderStyle = BorderStyle.FixedSingle;
            }
            else
            {
                panel.BorderStyle = BorderStyle.Fixed3D;
            }
            tbTableSelectedID.Text = panel.Name.ToString();
            //panel.Paint += new PaintEventHandler(View_TableAreaPaint);
            //panel.Refresh();
            //panel.BorderStyle = BorderStyle.Fixed3D;
            //panel.ForeColor = Color.Red;
            //panel.BackColor = Color.Green;
        }

        /// <summary>
        /// 预订开台
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void preOrderTableCMS_Click(object sender, EventArgs e)
        {
            //TODO: check tb不能为0的函数
            //MessageBox.Show(tbTableSelectedID.Text);
            IApplicationContext ctx = (this.MdiParent as MainForm).ApplicationContext;
            TableOrderForm toForm = new TableOrderForm(ctx, this);
            toForm.WindowState = FormWindowState.Normal;
            toForm.StartPosition = FormStartPosition.CenterScreen;
            toForm.TopMost = true;
            this.TopLevel = false;
            toForm.ShowDialog(this.MdiParent);
            toForm.Show();
        }

        /// <summary>
        /// 开台
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void preOrderCMS_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 清台
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearTableCMS_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void modifyStatus_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TableListForm_Resize(object sender, EventArgs e)
        {
            //左边
            panelMainLeft.Height = this.Height;
            //底部
            panelSystem.Width = this.Width;
            //右边
            panelMainRight.Width = this.Width - panelMainLeft.Width;
            panelMainRight.Height = this.Height - panelSystem.Height;
            //tablearea
            panelTableArea.Width = this.Width - panelMainLeft.Width;

            //TODO: 右侧桌面区域panel点选之后的边框的绘制
            //列出查询条件，餐桌状态
            IList<Xs_Table> tableList = tableService.GetAll();
            int left = panelTableArea.Left;
            int top = 5;//panelTableArea.Top;
            int width = panelTableArea.Width;//panelTableArea.Width;
            int height = panelTableArea.Height;

            int margin = 10;
            int tableWidth = 100;
            int tableHeight = 100;
            int lineNum = (width - margin) / (tableWidth + margin);

            for (int i = 0; i < tableList.Count; i++)
            {
                Xs_Table table = tableList.ElementAt<Xs_Table>(i);
                if (i % lineNum == 0 && i != 0)
                {
                    left = panelTableArea.Left;
                    top += tableHeight + margin;
                }
                else
                {
                    left = panelTableArea.Left + i % lineNum * tableWidth + i % lineNum * margin;
                }
                Panel panel = new Panel()
                {
                    Name = table.TableId.ToString(),//name保存桌台ID，作为传值参数
                    BorderStyle = BorderStyle.FixedSingle,
                    Margin = new Padding(margin, margin, margin, margin),
                    Padding = new Padding(margin),
                    Size = new Size(tableWidth, tableHeight),
                    Left = left,
                    Top = top,
                    Parent = panelTableArea,
                    ContextMenuStrip = cmsTable,
                    BackColor = ConvertTypeToColor(table.TableState),
                };
                panel.Click += new EventHandler(View_TableAreaClick);

                Label tableNameLabel = new Label() { Text = table.TableName + "\n状态:" + table.TableState + "\n" + "类别:4人桌" + "\n" + "总金额:100w", Parent = panel, TextAlign = ContentAlignment.MiddleCenter, AutoSize = false, Dock = DockStyle.Fill };
            }
        }
    }
}
